package com.bjsdzk.collector.timer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjsdzk.backend.rpc.serial.SerialPortService;
import io.vertx.core.json.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author sl
 */
@Component
@Profile({"test"/*,"dev"*/})
@Slf4j
public class TestTask {

    @Reference(check = false, version = "1.0.0", lazy = true, loadbalance = "allLoadBalance")
    private SerialPortService serialPortService;

    int randomCmdId=0;

    @Scheduled(cron = "0/30 * * * * ?")
    public void accessToken() {


        List<String> deviceIds = Arrays.asList("98CC4D000F52", "98CC4D20C6E1");
        String cmdId;
        List<String> cmdIds = Arrays.asList("A1", "A2");
        cmdId = cmdIds.get((randomCmdId++) % 2);
        for (int i = 0; i < deviceIds.size(); i++) {
            int port = 62008;
            String operation = "D3";
            String deviceHex = deviceIds.get(i);

            log.debug("deviceId:{},send cmd count:{}", deviceHex, randomCmdId);
            Integer deviceAddr = 1;
            String source = "platform";


            JsonObject json = new JsonObject();
            json.put("messageId", "sdlkfjasdklfjasldkjf");
            json.put("port", port);
            json.put("source", source);
            json.put("dtuType", "13");
            json.put("operation", operation);
            json.put("cmdId", cmdId);
            json.put("deviceHex", deviceHex);
            json.put("upperLimit", 0);
            json.put("lowerLimit", 0);
            json.put("value", 0);
            json.put("deviceAddr", deviceAddr);
            json.put("isInstantCmd", true);

            serialPortService.cmd(json.toString());
        }


    }
}
